﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using MapLibrary.GUI;

namespace GUILibrary
{
    /// <summary>
    /// This is a vertical bar that displays an amount of something.  Note that it does NOT fill on
    /// click, this is just for display.
    /// </summary>
    public class VerticalBar : Component
    {

        /// <summary>
        /// The underlying image of the bar.  This gets displayed under the
        /// fillImage.
        /// </summary>
        public string BaseImageName
        {
            get;
            set;
        }

        /// <summary>
        /// The image that will be repeated vertically to represent "filling"
        /// the bar.
        /// </summary>
        public string FillImageName
        {
            get;
            set;
        }

        /// <summary>
        /// The offset in the base image where we will start drawing the vertical
        /// bar.
        /// </summary>
        public Vector2 ProgressOffset
        {
            get;
            set;
        }

        /// <summary>
        /// How tall the "progress" area is.
        /// </summary>
        public int EffectiveHeight
        {
            get;
            set;
        }

        /// <summary>
        /// The value to track.  This value determines how full the bar is.
        /// </summary>
        [ContentSerializerIgnore]
        public int ValueToTrack
        {
            get { return toTrack.GetAmount(Name); }
        }

        [ContentSerializerIgnore]
        public ITrackable ToTrack
        {
            get { return toTrack; }
            set
            {
                toTrack = value;
                CheckAndTriggerInitialize();
            }
        }

        private ITrackable toTrack;

        /// <summary>
        /// The maximum value for the tracked value.  This determines what percent full
        /// the bar is.
        /// </summary>
        [ContentSerializerIgnore]
        public int MaximumValue
        {
            get { return toTrack.GetMax(Name); }
        }

        private Texture2D baseImage;
        private Texture2D fillImage;

        private int fillImageHeight;

        public override void Draw(SpriteBatch batch)
        {
            //draw the underlying bar at my position.
            batch.Draw(baseImage, ScreenPosition, Color.White);

            //now draw the fill amount
            float percent = CalculatePercent();
            int numBars = (int)((EffectiveHeight * percent) / fillImageHeight);
            Vector2 startPos = ScreenPosition + ProgressOffset;
            for (int i = 0; i < numBars; i++)
            {
                batch.Draw(fillImage, startPos - (i * new Vector2(0, fillImageHeight)), Color.White);
            }
        }

        public override void Load(ContentManager cm)
        {
            //initialize some values
            Action = "VerticalFillBar";

            baseImage = cm.Load<Texture2D>(BaseImageName);
            fillImage = cm.Load<Texture2D>(FillImageName);
            fillImageHeight = fillImage.Bounds.Height;
        }

        private float CalculatePercent()
        {
            if (!Initialized)
                return 0.0f;
            else
            {
                return (float)ValueToTrack / MaximumValue;
            }
        }

        private void CheckAndTriggerInitialize()
        {
            Initialized = toTrack != null;
        }
    }
}
